#ifndef _APPS_BDESKTOP_CDOCK_H_
#define _APPS_BDESKTOP_CDOCK_H_

#include "auto.h"
#include <buola/gui/cwindow.h>
#include <buola/gui/caccelerator.h>
#include <buola/widgets/cbutton.h>
#include <buola/gl/cgltexture.h>
#include "cdocktip.h"

namespace buola { namespace bdesktop {

class CDock : public gui::CWindow
{
    enum EValid
    {
        VALID_NONE=0,
        VALID_ZOOMRECT=(1<<0),
        VALID_RESTRECT=(1<<1),
        VALID_EFFRECT=(1<<2),
        VALID_ALL=0x07
    };
    DECLARE_FRIEND_ENUM_BITOPS(EValid)

    enum EAnimation
    {
        ANIMATE_NONE=0,
        ANIMATE_ZOOMIN=(1<<0),
        ANIMATE_ZOOMOUT=(1<<1),
        ANIMATE_OPACITYIN=(1<<2),
        ANIMATE_OPACITYOUT=(1<<3)
    };
    DECLARE_FRIEND_ENUM_BITOPS(EAnimation)
    
public:
    enum EGroup
    {
        GROUP_APPS,
        GROUP_MEDIA,
        GROUP_UTILS
    };

    enum EInsert
    {
        INSERT_BEFORE,
        INSERT_AFTER
    };
    
public:
    struct SIcon
    {
        class CDockIcon *mIcon;
        CRect mRestRect;
        CRect mZoomRect;
        CRect mEffRect;
        double mOpacityStep;
        EGroup mGroup;
        EAnimation mAnimating;
    };

public:
    CDock();
    
    void AddIcon(CDockIcon *pIcon,EGroup pGroup);
    void InsertIcon(CDockIcon *pIcon,EGroup pGroup,CDockIcon *pOther,
                                            EInsert pWhere=INSERT_AFTER);
    void RemoveIcon(CDockIcon *pIcon);

    void ShowMenu(gui::PMenu,const CPoint &pPos);

private:
    void Invalidate(EValid pWhat=VALID_ALL) {   mValid&=~pWhat;     }
    double GetZoom(double pPos,double pL,double pR);

    void StartAnimation(EAnimation pType);
    void StopAnimation(EAnimation pType);

    void RecalcRestRect();
    void RecalcZoomRect();
    void RecalcEffRect();
    
    void ShowTip(const CPoint &pPos,const std::wstring &pText);
    void HideTip();

    CGLTexture *GetBackTexture();

protected:

////////////// event handling //////////////
    virtual void OnCreate();
    virtual void OnButtonDown(gui::CMouseEvent*);
    virtual void OnMouseMove(gui::CMouseEvent*);
    virtual void OnMouseOut(gui::CMouseEvent*);
    virtual void OnPreExpose();
    virtual void OnExpose(gui::CExposeEvent*);
    virtual void OnSize(gui::CGeometryEvent*);
    virtual void OnKeyDown(gui::CKeyEvent *pE);
    void OnAnimationTimer();
    void OnMenuClosed();

protected:
    gui::CAccelerator mAltF2;

    std::vector<std::unique_ptr<SIcon>> mIcons;
    bool mMouseInside;
    CPoint mMousePos;
    CPoint mRootMousePos;

    CRect mIconsRect;
    
    double mZoomStep;

    double mHeight;
    EValid mValid;
    EAnimation mAnimating;

    scoped_ptr<CTimer> mTimer;

    scoped_ptr<CDockTip> mDockTip;
    int mTipIcon;

    gui::x11::CPixmap *mBackPixmap;
    CGLTexture *mBackTexture;

    bool mFrozen;
};

/*namespace bdesktop*/ } /*namespace buola*/ }

#endif
